package cn.ywsj.ywsjmidtier.controller;

import cn.ywsj.ywsjmidtier.service.MangeCodeFeign;
import cn.ywsj.ywsjmidtier.utils.CookiesUtil;
import feign.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
@Api(tags = "系统Code")
@RestController
@RequestMapping("captcha")
public class MangeCodeController{
    @Autowired
    private MangeCodeFeign mangeCodeFeign;

    /**
     * 系统Code
     * @return
     */
    @ApiOperation("系统Code")
    @PostMapping("getimageverifycode")
    public String getimageverifycode(HttpServletRequest request, HttpServletResponse response){
        Response feignResponse= mangeCodeFeign.getimageverifycode();
        Cookie[] cookies=request.getCookies();
        if(cookies==null) {
            Collection<String> cookieCollection=feignResponse.headers().get("set-cookie");
            String cookieStr= cookieCollection.toString();
            String jsessionidValue= CookiesUtil.getCookieBySet("JSESSIONID",cookieStr);
            Cookie cookie = new Cookie("JSESSIONID",jsessionidValue);
            cookie.setHttpOnly(true);
            cookie.setPath("/");
            cookie.setSecure(false);
            response.addCookie(cookie);
        }
        return feignResponse.body().toString();
    }
}
